package com.example.zyfypt_206shj.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zyfypt_206shj.R;
import com.example.zyfypt_206shj.bean.SessionID;
import com.example.zyfypt_206shj.bean.UserInfo;
import com.example.zyfypt_206shj.iface.LoginListener;
import com.example.zyfypt_206shj.model.LoginModel;

public class activity_login extends Activity implements View.OnClickListener,LoginListener {

    private ImageView imageView;//图片
    private EditText editText_usr;//用户名输入框
    private EditText editText_passwd;//密码输入框
    private Button button_login;//登录按钮
    private TextView register;//注册
    private TextView forgot_psw;//忘记密码
    private String username,password;
    SharedPreferences sp;
    private Switch sw;//switcher1

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏TITLE
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //设置TOOLBAR标题居中见xml中android.support.v7.widget.Toolbar控件
        setContentView(R.layout.activity_login);
        //初始化获取控件
        init();
    }

    public void init(){
        imageView = (ImageView) findViewById(R.id.iv);
        editText_usr = (EditText) findViewById(R.id.editText_usr);
        editText_passwd = (EditText) findViewById(R.id.editText_passwd);
        button_login = (Button) findViewById(R.id.login);
        register = (TextView) findViewById(R.id.register);
        forgot_psw = (TextView) findViewById(R.id.forgot_psw);
        sw=(Switch) findViewById(R.id.switch1);

        button_login.setOnClickListener(this);
        register.setOnClickListener(this);
        forgot_psw.setOnClickListener(this);
        sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(sw.isChecked()){
                    saveSP();
                }
                else{
                    editText_usr.setText("");
                    editText_passwd.setText("");
                    saveSP();
                }
            }
        });

        sp = getSharedPreferences("login" ,MODE_PRIVATE);
        readSP();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login:
                if(editText_usr.getText().toString().equals("") && editText_passwd.getText().toString().equals("")){
                    Toast.makeText(this, "没有账户的人是不让进的~", Toast.LENGTH_SHORT).show();
                    break;
                }
                //实例化LoginModel
                LoginModel loginModel = new LoginModel();
                //请求UserInfo
                loginModel.getUserInfo(editText_usr.getText().toString(),editText_passwd.getText().toString(), this);
                //然后这里转onResponse方法处理响应回来的数据
//                Intent intent = new Intent(activity_login.this, activity_main.class);
//                intent.putExtra("param1","hello,  ");
//                intent.putExtra("param2",5);
//                startActivity(intent);
                break;
            case R.id.register:
                Intent intent_reg = new Intent(activity_login.this, activity_regist.class);
                startActivity(intent_reg);
                break;
            case R.id.forgot_psw:
                Intent intent_fp = new Intent(activity_login.this, activity_forgotpasswd.class);
                startActivity(intent_fp);
                break;
            default:
                break;
        }
    }

    @Override
    public void onResponse(UserInfo userInfo) {
        //登录成功
//        Toast.makeText(this, userInfo.getId(), Toast.LENGTH_SHORT).show();
//        Toast.makeText(this, userInfo.getUsername(), Toast.LENGTH_SHORT).show();
        SessionID.sessionID = userInfo.getSessionid();//保存sessionID
        SessionID.id = userInfo.getId();
        SessionID.username = userInfo.getUsername();
        SessionID.realname = userInfo.getRealname();
        SessionID.rolename = userInfo.getRolename();
        SessionID.sex = userInfo.getSex();
        Intent intent = new Intent(activity_login.this, activity_main.class);
        startActivity(intent);
    }

    @Override
    public void onFail(String msg) {

    }

    private void readSP() {
        String name = sp.getString("username",null);
        String pass = sp.getString("password",null);
        Boolean b = sp.getBoolean("remember",false);
        if(b){
            editText_usr.setText(name);
            editText_passwd.setText(pass);
            sw.setChecked(b);
        }
    }

    private void saveSP() {
        SharedPreferences.Editor editor = sp.edit();
        username = editText_usr.getText().toString();
        password = editText_passwd.getText().toString();
        editor.putString("username",username);
        editor.putString("password",password);
        editor.putBoolean("remember",sw.isChecked());
        editor.commit();
    }
}
